package myaction.teacher;

import myutil.DbUtil;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class TeacherOptFindAll extends HttpServlet {

    public void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        doPost(request, response);
    }

    public void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        System.out.println("TeacherOptFindAll");
        request.setCharacterEncoding("utf-8");
        HttpSession session = request.getSession();
        String t_id = String.valueOf(session.getAttribute("teacher_id"));
        String c_isable = "0";
        List lstv = new ArrayList();
        try {
            Connection conn = DbUtil.getConn();
            String sql = "select c_id, c_name, DATE(c_time),c_isable" +
                    " from t_courses where c_teacher_id=" + t_id + ";";
            PreparedStatement ps = conn.prepareStatement(sql);
            ResultSet rs = ps.executeQuery();
            while (rs.next()) {
                c_isable = rs.getString("c_isable");
                if (c_isable == "1" || c_isable.equals("1")) {
                    c_isable = "未开课";
                } else {
                    c_isable = "已开课";
                }
                System.out.println("有值");
                List lstt = new ArrayList();
                lstt.add(rs.getInt("c_id"));
                lstt.add(rs.getString("c_name"));
                lstt.add(rs.getString("DATE(c_time)"));
                lstt.add(c_isable);
                lstv.add(lstt);
                System.out.println(lstt);
            }
            rs.close();
            ps.close();
            conn.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        request.setAttribute("lstv", lstv);
        request.getRequestDispatcher("/teacher/opt.jsp").forward(request,
                response);
    }

}
